package larpplanner.gui;

import larpplanner.database.Player;
import larpplanner.logic.LARPManagerImp;

import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.SWT;

public class SignUpWin extends org.eclipse.swt.widgets.Dialog implements SafeCallback<Boolean> {

	private Shell dialogShell;
	private Label emailLbl;
	private Label nameLbl;
	private Label phoneLbl;
	private Text passTxt;
	private Button okBtn;
	private Text phoneTxt;
	private Text nameTxt;
	private Text emailTxt;
	private Label passLbl;

	/**
	* Auto-generated main method to display this 
	* org.eclipse.swt.widgets.Dialog inside a new Shell.
	*/
	public static void main(String[] args) {
		try {
			Display display = Display.getDefault();
			Shell shell = new Shell(display);
			SignUpWin inst = new SignUpWin(shell, SWT.NULL);
			inst.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public SignUpWin(Shell parent, int style) {
		super(parent, style);
		initGUI();
		initLogic();
	}

	private void initLogic() {
		okBtn.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				Player player = new Player(nameTxt.getText(), phoneTxt.getText(), 
				                           emailTxt.getText(), passTxt.getText());

				GUISafeLARPManager.get().signUp(player, SignUpWin.this);
			}
		});
		
		FocusAdapter foc_adAdapter = new FocusAdapter() {
            public void focusGained(FocusEvent e) {
                ((Text) e.widget).setText("");
            }
        };
		
		nameTxt.addFocusListener(foc_adAdapter);
		emailTxt.addFocusListener(foc_adAdapter);
		phoneTxt.addFocusListener(foc_adAdapter);
		passTxt.addFocusListener(foc_adAdapter);
	}
	
	private void initGUI() {
		try {
			Shell parent = getParent();
			dialogShell = new Shell(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);

			GridLayout dialogShellLayout = new GridLayout();
			dialogShellLayout.numColumns = 2;
			dialogShell.setLayout(dialogShellLayout);
			dialogShell.setText("Sign up to LARP Planner");
			dialogShell.layout();
			dialogShell.pack();			
			dialogShell.setSize(275, 150);
			{
				emailLbl = new Label(dialogShell, SWT.NONE);
				GridData emailLblLData = new GridData();
				emailLbl.setLayoutData(emailLblLData);
				emailLbl.setText("email:");
			}
			{
				emailTxt = new Text(dialogShell, SWT.SINGLE);
				GridData emailTxtLData = new GridData();
				emailTxtLData.horizontalAlignment = GridData.FILL;
				emailTxtLData.grabExcessHorizontalSpace = true;
				emailTxt.setLayoutData(emailTxtLData);
				emailTxt.setText("email");
			}
			{
				nameLbl = new Label(dialogShell, SWT.NONE);
				GridData nameLblLData = new GridData();
				nameLbl.setLayoutData(nameLblLData);
				nameLbl.setText("name:");
			}
			{
				nameTxt = new Text(dialogShell, SWT.SINGLE);
				GridData nameTxtLData = new GridData();
				nameTxtLData.horizontalAlignment = GridData.FILL;
				nameTxtLData.grabExcessHorizontalSpace = true;
				nameTxt.setLayoutData(nameTxtLData);
				nameTxt.setText("name");
			}
			{
				phoneLbl = new Label(dialogShell, SWT.NONE);
				GridData phoneLblLData = new GridData();
				phoneLbl.setLayoutData(phoneLblLData);
				phoneLbl.setText("phone:");
			}
			{
				phoneTxt = new Text(dialogShell, SWT.SINGLE);
				GridData phoneTxtLData = new GridData();
				phoneTxtLData.horizontalAlignment = GridData.FILL;
				phoneTxtLData.grabExcessHorizontalSpace = true;
				phoneTxt.setLayoutData(phoneTxtLData);
				phoneTxt.setText("phone");
			}
			{
				passLbl = new Label(dialogShell, SWT.NONE);
				GridData passLblLData = new GridData();
				passLbl.setLayoutData(passLblLData);
				passLbl.setText("password:");
			}
			{
				passTxt = new Text(dialogShell, SWT.SINGLE | SWT.PASSWORD);
				GridData passTxtLData = new GridData();
				passTxtLData.horizontalAlignment = GridData.FILL;
				passTxtLData.grabExcessHorizontalSpace = true;
				passTxt.setLayoutData(passTxtLData);
				passTxt.setText("pass");
			}
			{
				okBtn = new Button(dialogShell, SWT.PUSH | SWT.CENTER);
				GridData okBtnLData = new GridData();
				okBtnLData.horizontalSpan = 2;
				okBtnLData.horizontalAlignment = GridData.FILL;
				okBtn.setLayoutData(okBtnLData);
				okBtn.setText("Confirm");
			}
		} catch (Exception e) {
				e.printStackTrace();
		}
	}
	
	public void open() {
		try{
			dialogShell.setLocation(getParent().toDisplay(100, 100));
			dialogShell.open();
			Display display = dialogShell.getDisplay();
			while (!dialogShell.isDisposed()) {
				if (!display.readAndDispatch())
					display.sleep();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

    @Override
    public void onReturn(Boolean isSuccess) {
        if(!isSuccess)
            PopMessage.errorMessage("error signing up");
        dialogShell.dispose();
    }
	
}
